using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class tbDiaDiemDAL
    {
        private SqlConnection cn = null;
        public tbDiaDiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbDiaDiem dd)
        {
            SqlCommand cm = new SqlCommand("MaSoSP_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@DiaDiem  ", dd.DiaDiem);
            cm.ExecuteNonQuery();

        }
        public void Update(tbDiaDiem dd)
        {
            SqlCommand cm = new SqlCommand("MaSoSP_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@DiaDiem  ", dd.DiaDiem);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("MaSoSP_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDDiaDiem  ", Id);
            cm.ExecuteNonQuery();
        }
        public tbDiaDiem.tbDiaDiemCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("MaSoSP_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbDiaDiem.tbDiaDiemCollection ddcollection = new tbDiaDiem.tbDiaDiemCollection();
            while (dr.Read())
            {
                tbDiaDiem dd = new tbDiaDiem();
                dd.IDDiaDiem = int.Parse(dr.GetValue(0).ToString());
                dd.DiaDiem = dr.GetValue(1).ToString();
                ddcollection.Add(dd);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ddcollection;
        }

    }
}
